/**
 * Created by zhushoude on 2017/5/18.
 */
var username=localStorage.getItem("username");
var token=localStorage.getItem("token");
var userid=localStorage.getItem("userid");
$(function(){
    $("#deadLine").datetimepicker({lang:'ch',
        timepicker:false,
        format:'Y-m-d',
        formatDate:'Y-m-d'
    });
    InitParentCate();
    //父分类切换
    $("#cateIdFirst").change(function(){
        var parentId=$(this).val();
        changeChildKv(parentId);
    });
    //类别初始化
    InitcategoryID();
    var paramMap = params();
    var id = paramMap['id'];
    $("#backList").click(function(){
        var url=server_url+'/demand/list.do';
        tl_config.last_url=url;
        tl_config.loadToWorkArea(url);
    });

    $.ajax({
        type : 'GET',
        contentType : 'application/json',
        async : false,
        data : {
            demandId : id,
            token:token
        },
        url : server_url+'/rest/demand/getDemand.do?ran='+Math.random(),
        dataType : 'json',
        success : function(data) {
            if (data.retCode == "0000") {
                pasteData(data.data);
            } else{
                easyDialog.open({
                    container:{
                        header:'温馨提示',
                        content:data.retMsg
                    },
                    autoClose : 2000
                });
            }
        }
    });
    $("input[type=file]").change(function(){
        ajaxfileuploadUtil(this,"demand");
    });

    $("#save").click(function(){
        if(!$("form").valid()){
            return;
        }
        $.ajax({
            type:'post',
            url:server_url+'/rest/demand/modifyDemand.do?token='+token+'&ran='+Math.random(),
            data:$("form").serialize(),
            success:function(data){
                easyDialog.open({
                    container : {
                        header : '温馨提示',
                        content : '保存成功',
                        yesFn:function(){
                            var url=server_url+'/demand/list.do';
                            tl_config.last_url=url;
                            tl_config.loadToWorkArea(url);
                        }
                    },
                });
            }
        });
    });



});

function isEmpty(data){
    if(data==null||data==undefined||data.length==0){
        return true;
    }else{
        return false;
    }
}
function pasteData(data){
    var demand=data.demand.data;
    var buyer=demand.buyer;

    $("#id").val(demand.details[0].demandId);
    $("#demandId").val(demand.details[0].demandId);
    $("#demandCode").text(demand.demandCode);
    $("#usersName").text(demand.usersName);
    $("#buyerTel").text(buyer.buyerTel);
    $("#productName").val(demand.details[0].productName);
    $("[name=imgUrl]").val(demand.details[0].imgUrl);
    $("#imgUrl").attr("src",demand.details[0].imgUrl);

    $("#cateIdFirst").val(demand.details[0].cateIdFirst);

       changeChildKv(demand.details[0].cateIdFirst);

    $("#cateIdSeconde").val(demand.details[0].cateIdSeconde);


    $("#categoryId").val(demand.details[0].categoryId);

    $("#minPrice").val(demand.details[0].minPrice);
    $("#maxPrice").val(demand.details[0].maxPrice);
    $("#deliveryArea").val(demand.details[0].deliveryArea);
    $("#deliveryAddress").val(demand.details[0].deliveryAddress);
    $("#deadLine").val(demand.deadLine);
    $("#amount").val(demand.details[0].amount);
    $("#isOnLine").val(demand.isOnLine);

    if(demand.details[0].isRequireFile==1){
        $("#isRequireFile").attr("checked", true);
    }
    else
    {
        $("#isRequireFile").attr("checked", false);
    }
    if(demand.details[0].isRequireImg==1){
        $("#isRequireImg").attr("checked", true);
    }
    else
    {
        $("#isRequireImg").attr("checked", false);
    }
    if(demand.details[0].isRegister==1){
        $("#isRegister").attr("checked", true);
    }
    else
    {
        $("#isRegister").attr("checked", false);
    }
    if(demand.details[0].isChild==1){
        $("#isChild").attr("checked", true);
    }
    else
    {
        $("#isChild").attr("checked", false);
    }
    mapIfExist(function(dt){
        $("<option value="+dt.dicValue+">"+dt.dicName+"</option>").appendTo("#approveStatus");
    }, data.approveStatus);
    $("#approveStatus").val(demand.approveStatus);

    $("#remark").text(demand.details[0].remark);

};
function back(){
    var url=server_url+'demand/list.do';
    tl_config.loadToWorkArea(url);
}


//初始化类别列表
function InitcategoryID() {
    $.ajax({
        url:server_url+'/rest/demand/getCateId.do?ran='+Math.random()+"&token="+token,
        type:'GET',
        async:false,
        success:function(data){

            if(data.retCode=="0000"){
                for(var i=0;i<data.data.data.length;i++){
                    $("#categoryId").append("<option value='"+data.data.data[i].specValueId+"'>"+data.data.data[i].specValueName+"</option>");
                }
            }
            else{
                easyDialog.open({
                    container : {
                        header : '提示',
                        content : data.retMsg
                    },
                    autoClose : 2000
                });
            }
        }
    });
}


//初始化下拉列表
function InitParentCate()
{
    $.ajax({
        url:server_url+'/rest/productCate/selectParentCate.do?ran='+Math.random()+"&token="+token,
        type:'get',
        async:false,
        success:function(data){
            if(data.retCode=="0000"){
                for(var i=0;i<data.data.length;i++){
                    $("#cateIdFirst").append("<option value='"+data.data[i].cateId+"'>"+data.data[i].cateName+"</option>");
                }
            }
            else{
                easyDialog.open({
                    container : {
                        header : '提示',
                        content : data.retMsg
                    },
                    autoClose : 2000
                });
            }
        }
    });
}

//切换子级分类
function changeChildKv(parentId){
    $("#cateIdSeconde").empty();
    $.ajax({
        url:server_url+'/rest/productCate/selectChildCateKv.do?ran='+Math.random()+"&token="+token,
        type:'post',
        async:false,
        data:{cateId:parentId},
        success:function(data){
            if(data.retCode=="0000"){
                for(var i=0;i<data.data.length;i++){
                    $("#cateIdSeconde").append("<option value='"+data.data[i].cateId+"'>"+data.data[i].cateName+"</option>");
                }
            }
            else{
                easyDialog.open({
                    container : {
                        header : '提示',
                        content : data.retMsg
                    },
                    autoClose : 2000
                });
            }
        }
    });
}


//刷新当前页面
var reloadFn = function(){
    window.location.reload();
};
